package com.tduck.cloud.form.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.tduck.cloud.form.entity.UserFormDataEntity;
import com.tduck.cloud.form.vo.FormReportVO;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Set;

/**
 * 表单报表
 *
 * @author : smalljop
 * @description :
 * @create :  2020/12/31 14:51
 **/
public interface FormDashboardMapper extends BaseMapper<UserFormDataEntity> {


    /**
     * 查询当周的每天数量
     * WHERE  YEARWEEK( date_format( create_time, '%Y-%m-%d' )) = YEARWEEK( now()) and Form_key=#{formKey}
     * @param formKey
     * @return
     */
    @Select("<script>" +
            " SELECT date_format( create_time, '%Y-%m-%d' ) as create_time, COUNT( 1 ) AS count FROM fm_user_form_data" +
            " WHERE Form_key = #{formKey}" +
            "<when test='queryData != null'>" +
            "   and original_data like CONCAT('%', #{queryData}, '%') " +
            "</when>" +
            "<when test='beginDate != null'>" +
            "   and create_time &gt;= #{beginDate}" +
            "</when>" +
            "<when test='endDate != null'>" +
            "   and create_time &lt; #{endDate}" +
            "</when>" +
            "GROUP BY date_format( create_time, '%Y-%m-%d' )" +
            "</script>")
    Set<FormReportVO.Situation> selectFormReportSituation(@Param("formKey") String formKey,@Param("queryData") String queryData,@Param("beginDate") String beginDate, @Param("endDate") String endDate);


    /**
     * 分组查询位置
     *
     * @param formKey
     * @return
     */
    @Select("<script>" +
            "SELECT  submit_address,COUNT(1) as count FROM fm_user_form_data WHERE form_key = #{formKey} " +
            "<when test='queryData != null'>" +
            "   and original_data like CONCAT('%', #{queryData}, '%') " +
            "</when>" +
            "<when test='beginDate != null'>" +
            "   and create_time &gt;= #{beginDate}" +
            "</when>" +
            "<when test='endDate != null'>" +
            "   and create_time &lt; #{endDate}" +
            "</when>" +
            "GROUP BY submit_address" +
            "</script>")
    List<FormReportVO.Position> selectFormReportPosition(@Param("formKey") String formKey, @Param("queryData") String queryData, @Param("beginDate") String beginDate, @Param("endDate") String endDate);


    /**
     * 设备
     *
     * @param formKey
     * @return
     */
    @Select("<script>" +
            "SELECT  submit_os as os_name,COUNT(1) as count FROM fm_user_form_data WHERE form_key = #{formKey} " +
            "<when test='queryData != null'>" +
            "   and original_data like CONCAT('%', #{queryData}, '%') " +
            "</when>" +
            "<when test='beginDate != null'>" +
            "   and create_time &gt;= #{beginDate}" +
            "</when>" +
            "<when test='endDate != null'>" +
            "   and create_time &lt; #{endDate}" +
            "</when>" +
            " GROUP BY submit_os" +
            "</script>")
    List<FormReportVO.Device> selectFormReportDevice(@Param("formKey") String formKey, @Param("queryData") String queryData, @Param("beginDate") String beginDate, @Param("endDate") String endDate);


    /**
     * 来源
     *
     * @param formKey
     * @return
     */
    @Select("<script>" +
            "SELECT * FROM fm_user_form_data WHERE form_key = #{formKey} " +
//            "<when test='queryData != null'>" +
//            "   and original_data like CONCAT('%', #{queryData}, '%') " +
//            "</when>" +
            "<when test='beginDate != null'>" +
            "   and create_time &gt;= #{beginDate}" +
            "</when>" +
            "<when test='endDate != null'>" +
            "   and create_time &lt; #{endDate}" +
            "</when>" +
            "</script>")
    List<UserFormDataEntity> selectFormReportSource(@Param("formKey") String formKey, @Param("beginDate") String beginDate, @Param("endDate") String endDate);
}
